using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Ltf.Remoting.Settings;
using Ltf.Remoting.TeamFoundation;
using Microsoft.TeamFoundation.VersionControl.Client;
using Lizard.Remoting.Settings;

namespace Ltf.MonitorLizard
{
	// just a comment

	public partial class LizardSettings : UserControl
	{

		public event EventHandler NewSettings;
		public event EventHandler RemoveServer;

		private TabPage page;
		private TFSecondarySettings settings;
		private TFSettings primarySettings;

		public LizardSettings(TabPage page, TFSettings primarySettings, bool primaryOnly)
			: this (page, null)
		{
			this.primarySettings = primarySettings;
		}
		
		public LizardSettings(TabPage page, TFSecondarySettings settings)
		{
			InitializeComponent();
			this.page = page;
			this.settings = settings;
			if (settings !=null)
				SetControlValues(settings);
			if (settings!=null)
				this.primarySettings = settings.PrimarySettings;
		}

		public void SetControlValues(TFSecondarySettings tfs)
		{
			txtUsername.Text = tfs.User;
			txtPassword.Text = tfs.Password;
			txtServerUrl.Text = tfs.Server;
			txtDomain.Text = tfs.Domain;
			txtServerPaths.Text = tfs.ServerPaths;
			txtDefaultWorkingPath.Text = tfs.DefaultWorkingPath;
			ckbAutoCheckOut.Checked = tfs.AutoCheckoutChangedFiles;

		}

		public void GetControlValues(TFSecondarySettings tfs)
		{
			tfs.User = txtUsername.Text;
			tfs.Password = txtPassword.Text;
			tfs.Server = txtServerUrl.Text;
			tfs.Domain = txtDomain.Text;
			tfs.ServerPaths = txtServerPaths.Text;
			tfs.DefaultWorkingPath = txtDefaultWorkingPath.Text;
			//tfs.LtfMetaFolderName = txtMetaFolderName.Text;
			//tfs.IncludedDrives = txtIncludedDrives.Text;
			//tfs.Excludedfolders = txtExcludedFolders.Text;
			//tfs.Excludedfiles = txtExcludedFiles.Text;
			//tfs.EditTool = txtEditorPath.Text;
			//tfs.TempPath = txtTempPath.Text;
			//tfs.X64InstallPath = txtX64InstallPath.Text;
			tfs.AutoCheckoutChangedFiles = ckbAutoCheckOut.Checked;

		}

		public bool TestControlValues()
		{

			TFSecondarySettings tfs = settings;
			if (tfs == null)
				tfs = new TFSecondarySettings(primarySettings);
			tfs.User = txtUsername.Text;
			tfs.Password = txtPassword.Text;
			tfs.Domain = txtDomain.Text;
			tfs.Server = txtServerUrl.Text;
			tfs.ServerPaths = txtServerPaths.Text;
			//tfs.LtfMetaFolderName = txtMetaFolderName.Text;
			//tfs.IncludedDrives = txtIncludedDrives.Text;
			//tfs.Excludedfolders = txtExcludedFolders.Text;
			//tfs.Excludedfiles = txtExcludedFiles.Text;
			//tfs.EditTool = txtEditorPath.Text;
			//tfs.TempPath = txtTempPath.Text;
			//tfs.X64InstallPath = txtX64InstallPath.Text;
			tfs.DefaultWorkingPath = txtDefaultWorkingPath.Text;
			tfs.AutoCheckoutChangedFiles = ckbAutoCheckOut.Checked;

			try
			{
				Ltf.Remoting.Settings.SettingsFactory.SaveSettings("Server", tfs.PrimarySettings);

				TFServerConnection connection = new TFServerConnection(tfs);

				VersionControlServer vcServer = connection.VersionControlServer;

				TeamProject[] projects = vcServer.GetAllTeamProjects(true);
				List<string> projectNames = new List<string>();
				foreach (TeamProject tp in projects)
					projectNames.Add(tp.Name);
				MessageBox.Show("Settings founds the following team projects :" + string.Join(", ", projectNames.ToArray()), "Settings Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
				//if (tfs.AutoCheckoutChangedFiles && Directory.Exists(txtDefaultWorkingPath.Text))
				//{
				//  CreateFileSystemWatcher();
				//}

			}
			catch (Exception ex)
			{
				MessageBox.Show("Settings failed test with the error :" + ex.Message, "Settings Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}
			return true;
		}

		private void label3_Click(object sender, EventArgs e)
		{

		}

		private void txtRemove_Click(object sender, EventArgs e)
		{
			if (RemoveServer != null)
				RemoveServer(this, new LizardSettingsEventArgs(null, page));
		}

		private void btnTestAndSave_Click(object sender, EventArgs e)
		{
			if (TestControlValues())
			{
				TFSecondarySettings tfs = settings;
				if (tfs==null)
					tfs = new TFSecondarySettings(this.primarySettings);
				GetControlValues(tfs);
				if (NewSettings != null)
					NewSettings(this, new LizardSettingsEventArgs(tfs, page));
			}
		}

		private void txtUsername_TextChanged(object sender, EventArgs e)
		{

		}

		private void btnWorkingPath_Click(object sender, EventArgs e)
		{
			GetFolderPath(txtServerPaths);
		}

		private void GetFolderPath(TextBox txtFolderBox)
		{
			FolderBrowserDialog fbd = new FolderBrowserDialog();
			fbd.SelectedPath = "";


			if (fbd.ShowDialog() == DialogResult.OK)
				txtFolderBox.Text += fbd.SelectedPath;
		}

		private void btnDefaultFolderPicker_Click(object sender, EventArgs e)
		{
			GetFolderPath(txtDefaultWorkingPath);
		}
	}

	public class LizardSettingsEventArgs : EventArgs
	{
		public TFSecondarySettings Settings;
		public TabPage Page;

		public LizardSettingsEventArgs(TFSecondarySettings settings, TabPage page)
		{
			Settings = settings;
			Page = page;
		}
	}
}
